﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

using DotNetNuke.Common.Utilities;
using DotNetNuke.Framework.Providers;

namespace CharityGamesOnline.Modules.Game
{
    /// ----------------------------------------------------------------------------- 
    /// <summary> 
    /// SQL Server implementation of the abstract DataProvider class 
    /// </summary> 
    /// <remarks> 
    /// </remarks> 
    /// <history> 
    /// </history> 
    /// ----------------------------------------------------------------------------- 
    public class SqlDataProvider : DataProvider
    {


        #region "Private Members"

        private const string ProviderType = "data";
        private const string ModuleQualifier = "CGO_";

        private ProviderConfiguration _providerConfiguration = ProviderConfiguration.GetProviderConfiguration(ProviderType);
        private string _connectionString;
        private string _providerPath;
        private string _objectQualifier;
        private string _databaseOwner;

        #endregion

        #region "Constructors"

        public SqlDataProvider()
        {

            // Read the configuration specific information for this provider 
            Provider objProvider = (Provider)_providerConfiguration.Providers[_providerConfiguration.DefaultProvider];

            // Read the attributes for this provider 

            //Get Connection string from web.config 
            _connectionString = Config.GetConnectionString();

            if (_connectionString == "")
            {
                // Use connection string specified in provider 
                _connectionString = objProvider.Attributes["connectionString"];
            }

            _providerPath = objProvider.Attributes["providerPath"];

            _objectQualifier = objProvider.Attributes["objectQualifier"];
            if (_objectQualifier != "" & _objectQualifier.EndsWith("_") == false)
            {
                _objectQualifier += "_";
            }

            _databaseOwner = objProvider.Attributes["databaseOwner"];
            if (_databaseOwner != "" & _databaseOwner.EndsWith(".") == false)
            {
                _databaseOwner += ".";
            }

        }

        #endregion

        #region "Properties"

        public string ConnectionString
        {
            get { return _connectionString; }
        }

        public string ProviderPath
        {
            get { return _providerPath; }
        }

        public string ObjectQualifier
        {
            get { return _objectQualifier; }
        }

        public string DatabaseOwner
        {
            get { return _databaseOwner; }
        }

        #endregion

        #region "Private Methods"

        private string GetFullyQualifiedName(string name)
        {
            return DatabaseOwner + ObjectQualifier + ModuleQualifier + name;
        }

        private object GetNull(object Field)
        {
            return DotNetNuke.Common.Utilities.Null.GetNull(Field, DBNull.Value);
        }

        #endregion

        #region "Game Methods"
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="portalID"></param>
        /// <param name="gameID"></param>
        /// <returns></returns>
        public override IDataReader GetGame(int gameID)
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName("GameGet"), gameID);
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="portalID"></param>
        /// <returns></returns>
        public override IDataReader GetGames(int portalID, int eventID)
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName("GameGetList"), portalID, eventID);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="gameID"></param>
        /// <param name="portalID"></param>
        /// <param name="userID"></param>
        /// <param name="status"></param>
        /// <param name="name"></param>
        /// <param name="description"></param>
        /// <param name="imageURL"></param>
        /// <param name="imageType"></param>
        /// <param name="imageWidth"></param>
        /// <param name="imageHeight"></param>
        /// <param name="gameURL"></param>
        /// <param name="gameType"></param>
        /// <param name="gameWidth"></param>
        /// <param name="gameHeight"></param>
        /// <param name="category"></param>
        /// <param name="imageDisplay"></param>
        public override void SaveGame(int gameID, int portalID, int userID, int status, string name, string description,
                                        string imageURL, string imageType, int imageWidth, int imageHeight,
                                        int gameTabID, int category, bool imageDisplay)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName("GameSave"),
                gameID, portalID, userID,status, name, GetNull(description), 
                GetNull(imageURL), GetNull(imageType), GetNull(imageWidth), GetNull(imageHeight), 
                GetNull(gameTabID),GetNull(category), imageDisplay);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="portalID"></param>
        /// <param name="gameID"></param>
        public override void DeleteGame(int gameID)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName("GameDelete"), gameID);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PortalId"></param>
        /// <returns></returns>
        public override IDataReader GetGamePackages()
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName("GameGetPackageList"));
        }

        public override IDataReader GetGamePackage(int packageID)
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName("GameGetPackage"), packageID);
        }

        #endregion

        #region "GameEnrollment Methods"

        public override IDataReader GetEnrollment(int portalID, int enrollmentID)
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName("GameEnrollmentGet"), portalID, enrollmentID);
        }

        public override IDataReader GetEnrollments(int portalID, int eventID, int gameID, int userID, int enrollStatusID, 
                                                   bool getCount, int pageSize, int currentPage)
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName("GameEnrollmentGetList"), 
                                                    portalID, eventID, gameID, userID, enrollStatusID, 
                                                    getCount, pageSize, currentPage);
        }

        public override IDataReader GetEnrollmentsByUser(int portalID, int userID, bool getCount, int pageSize, int currentPage)
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName("GameEnrollmentGetListByUser"),
                                                    portalID, userID, getCount, pageSize, currentPage); 
        }

        public override int SaveEnrollment(int enrollmentID, int portalID, int eventID, int gameID, int userID, int enrollStatusID,
                                            decimal enrollFee, DateTime startTime, DateTime endTime, int connectCount, decimal score)
        {            
            object retObj = SqlHelper.ExecuteScalar(ConnectionString, GetFullyQualifiedName("GameEnrollmentSave"), 
                                enrollmentID, portalID, eventID, gameID, userID, enrollStatusID, 
                                GetNull(enrollFee), GetNull(startTime), GetNull(endTime), GetNull(connectCount), GetNull(score));
            
            return Convert.ToInt32(retObj);
        }
        
        public override void DeleteEnrollment(int portalID,int enrollmentID)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName("GameEnrollmentDelete"), portalID, enrollmentID);
        }

        public override int GetPlayedCount(int portalID, int eventID, int gameID, int userID, int enrollStatusID)
        {
            object retObj = SqlHelper.ExecuteScalar(ConnectionString, GetFullyQualifiedName("GameEnrollmentGetPlayedCount"), 
                                                    portalID, eventID, gameID, userID, enrollStatusID);
            return Convert.ToInt32(retObj);
        }

        public override decimal GetMoneyCount(int portalID, int eventID, int gameID, int userID, int enrollStatusID)
        {
            object retObj = SqlHelper.ExecuteScalar(ConnectionString, GetFullyQualifiedName("GameEnrollmentGetMoneyCount"), 
                                                    portalID, eventID, gameID, userID, enrollStatusID);
            return Convert.ToDecimal(retObj);
        }

        public override int GetUserRank(int enrollID, int userID)
        {
            object retObj = SqlHelper.ExecuteScalar(ConnectionString, GetFullyQualifiedName("GameEnrollmentGetRank"), enrollID, userID);
            return Convert.ToInt32(retObj);
        }
        #endregion

    }
}
